/* *****************************************************************************
 *
 *	danizone.ch
 *
 *	Developer:		mda010
 *	Creation date:	May 6, 2012 10:09:17 PM
 *	
 *	Project name:	HomegrownCuisine
 *	File name:		AdminController.java
 *
 *	Copyrights (C) 2012 @danizone.ch
 *
 *   This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *	GNU General Public License for more details.
 *
 *	Change history:	[developer]; [date];[reason for change]
 *
 *					mda010; May 6, 2012; created
 *
 ***************************************************************************** */
package ch.zhaw.hgc.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;

import ch.zhaw.hgc.entities.Recipe;
import ch.zhaw.hgc.services.ICategroyService;
import ch.zhaw.hgc.services.IRecipeService;
import ch.zhaw.hgc.services.ISeasonService;

/**
 * @author Remo Zollinger Description: Provides the basic functionality to add a
 *         recipe.
 */
@Controller
@RequestMapping("/admin/addRecipe")
public class AddRecipeController {

	private static final Logger logger = Logger
			.getLogger(AddRecipeController.class);

	@Autowired
	private IRecipeService recipeService;

	@Autowired
	private ICategroyService categoryService;

	@Autowired
	private ISeasonService seasonService;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws ServletException {
		// to actually be able to convert Multipart instance to a String
		// we have to register a custom editor
		binder.registerCustomEditor(byte[].class,
				new ByteArrayMultipartFileEditor());
		//binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
		// now Spring knows how to handle multipart object and convert them
	}

	/**
	 * Get Method for recipe.
	 * 
	 * @param recipe
	 *            a Recipe
	 * @return the Model view
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView get(Recipe recipe) {
		ModelAndView model = new ModelAndView("admin/addRecipe");
		model.addObject("recipe", recipe);
		model.addObject("message", "");
		model.addObject("categories", categoryService.findAll());
		model.addObject("seasons", seasonService.findAll());
		model.addObject("portions", getPortions());

		logger.info("get()...");

		return model;
	}

	/**
	 * Post Method adding a recipe.
	 * 
	 * @param recipe
	 *            the recipe
	 * @return the Model view
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String submit(@ModelAttribute("recipe") Recipe recipe) {

		User user = (User) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		recipe.setAuthor(user.getUsername());

		recipeService.save(recipe);

		logger.info("submit()...");

		
		
		return "redirect:/";
	}
	
	private List<String> getPortions() {
		List<String> portions = new ArrayList<String>();
		portions.add("");
		for(int i = 1;i<=10;i++) {
			portions.add(String.valueOf(i));
		}
		return portions;
	}
}